﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessTempCareUnitManagement
// 文件功能描述：临时卡单位管理数据访问层
//
// 
// 创建标识：2012-05-17 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessTempCareUnitManagement
    {
        /// <summary>
        /// 获取临时卡单位数量
        /// </summary>
        /// <returns></returns>
        public int GetTempCardUnitcount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from File_Department";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取临时卡单位数量
        /// </summary>
        /// <returns></returns>
        public int GetTempCardUnitcount(string strquery)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from File_Department " + strquery;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取临时卡单位信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelTempCardUnitInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY Dep_ID ASC) rownumber,* " +
                               "from File_Department " + strquery + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 删除临时卡单位
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteTempCardUnit(string ID)
        {
            int rowcount = 0;
            sqldb db = new sqldb();
            string query = "select count(1) from FILE_PERSONNEL where Dep_ID='" + ID + "'";
            object obj = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            if (Convert.ToInt32(obj) == 0)
            {
                query = "Delete from File_Department where Dep_ID = '" + ID + "'";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            else
            {
                rowcount = 0;
            }
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条临时卡单位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataSet GetTempCardUnitInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from File_Department where Dep_ID = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 保存临时卡单位(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveTempCardUnitAdd(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "select MAX(Dep_No)+1 from File_Department";
            object obj = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            query = "insert into File_Department(Dep_No,deptName) Values('" +
                obj + "','" + supp[0] + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 保存临时卡单位(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveTempCardUnitEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update File_Department set deptName='" + supp[1] + "' where Dep_ID=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }
    }
}
